package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.dao.IDisplayZoneDao;
import cn.edu.sjtu.se.dslab.dao.IDomainEntityDao;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.project.DisplayZone;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IDisplayZoneService;

@Service("displayZoneService")
@Transactional
public class DisplayZoneServiceImpl implements IDisplayZoneService {

	@Resource(name = "displayZoneDao")
	private IDisplayZoneDao displayZoneDao;
	
	@Resource(name = "domainEntityDao")
	private IDomainEntityDao domainEntityDao;
	
	@Resource(name="projectDao")
	private IProjectDao projectDao;
	
	@Override
	public boolean addDisplayZone(DisplayZone displayZone, long projectId) {
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project id not found " + projectId);
		displayZone.setProject(project);
		displayZoneDao.save(displayZone);
		return true;
	}

	@Override
	public DisplayZone updateDisplayZone(DisplayZone displayZone) {
		return displayZoneDao.update(displayZone);
	}

	@Override
	public DisplayZone getDisplayZoneById(long id) {
		return displayZoneDao.queryById(id);
	}

	@Override
	public DisplayZone getDisplayZoneByName(String name) {
		return displayZoneDao.queryByName(name);
	}

	@Override
	public List<DisplayZone> getDisplayZoneList() {
		return displayZoneDao.queryByList();
	}

	@Override
	public boolean deleteDisplayZoneById(long displayZoneId) {
		DisplayZone displayZone = displayZoneDao.queryById(displayZoneId);
		if (null != displayZone) {
			displayZoneDao.delete(displayZone);
			return true;
		}
		return false;
	}

	@Override
	public List<DisplayZone> getDisplayZoneByProject(long projectId) {
		return displayZoneDao.queryByProject(projectId);
	}
	
	@Override
	public DisplayZone getDisplayZoneByProjectAndName(long projectId, String name){
		return displayZoneDao.queryByProjectAndName(projectId, name);
	}

	@Override
	public DomainEntity getDomainEntity(long displayZoneId) {
		DisplayZone displayZone = getDisplayZoneById(displayZoneId);
		if (displayZone == null) throw new NullPointerException();
		String entityUri = displayZone.getEntityUri();
		if(entityUri == null || entityUri.equals("")) return null;
		DomainEntity domainEntity = domainEntityDao.queryByUri(entityUri);
		return domainEntity;
	}
}

 

















